<?php
namespace app\shop\model;

use app\admin\model\ShopRefundOrderLog;
use app\crm\model\Customer;
use app\crm\model\FollowRecord;
use app\admin\model\Common;
use Kingdee\Kingdee;
use think\facade\Db;
use think\model\concern\SoftDelete;
use app\common\traits\ActionRecordTrait;
use app\shop\model\RefundPayModel;

class AddOrderLog extends Common
{
    use ActionRecordTrait;

    /**

     * 表名

     * @var string

     */

    protected $name = 'shop_refund_order_log';

    protected $defaultSoftDelete = 0;

    /**

     * 获取sku信息

     * @return \think\model\relation\hasOne

     * @author SL

     * @create 2021-05-27 15:04

     */

    public function orderGoods()

    {

        return $this->hasOne(Goods::class,'id');

    }

    // 新增客户消费记录
    public function addData($customer_id,$shop_id,$order_id,$deal_time,$order_type){


        $shopRefundOrderLog = new ShopRefundOrderLog();
        $shopRefundOrderLog->customer_id = $customer_id;
        $shopRefundOrderLog->shop_id = $shop_id;
        $shopRefundOrderLog->order_no = $order_id;
        $shopRefundOrderLog->order_type = $order_type;
        $shopRefundOrderLog->deal_time = $deal_time;
        $shopRefundOrderLog->create_time = time();
        $shopRefundOrderLog->save();
        // 修改客户最新消费时间
        $this->updateFinalConsumption($customer_id);
        return true;
    }

    // 撤销顾客消费记录
    public function deletedData($customer_id,$order_id){

        $where = [];
        array_push($where,['order_no','=',$order_id]);
        ShopRefundOrderLog::where($where)->update(['is_deleted' => 1]);

        // 修改客户最新消费时间
        $this->updateFinalConsumption($customer_id);
    }

    // 修改客户最新消费时间
    public function updateFinalConsumption($customer_id)
    {
        $where = [];
        array_push($where,['customer_id','=',$customer_id]);
        array_push($where,['is_deleted','=',0]);
        $finalConsumptionTime = ShopRefundOrderLog::where($where)->field('max(deal_time) as deal_time')->find();
        if($finalConsumptionTime){
            Customer::where('id',$customer_id)->update(['final_consumption' => $finalConsumptionTime['deal_time']]);
        }
        return true;
    }

}


?>
